﻿// -----------------------------------------------------------------------
// <copyright file="Utility.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Encryption
{
    using System.IO;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Utility
    {
        #region ReadFile

        public bool ReadFileToString( string filePathIn, ref string fileContent )
        {
            bool bResult = false;
            fileContent = string.Empty;

            if ( File.Exists( filePathIn ) )
            {
                try
                {
                    fileContent = File.ReadAllText( filePathIn );
                    bResult = true;
                }
                catch ( System.Exception ) 
                {
                    // Log exception
                }
            }

            return bResult;
        }

        #endregion

        #region ReadFile to byte

        public bool ReadFileTobyte( string filePathIn, ref byte[] filebyteContent )
        {
            bool bResult = false;
            string fileContent = string.Empty;

            if ( File.Exists( filePathIn ) )
            {
                try
                {
                    fileContent = File.ReadAllText( filePathIn );
                    filebyteContent = Encoding.UTF8.GetBytes( fileContent );
                    bResult = true;
                }
                catch ( System.Exception )
                {
                    // Log exception
                }
            }

            return bResult;
        }

        #endregion

        #region Write File By String
        public bool WriteFileByString( string filePathOut, string fileContent )
        {
            bool bResult = false;
            try
            {
                File.WriteAllText( filePathOut, fileContent );
                bResult = true;
            }
            catch ( System.Exception ) { }

            return bResult;
        }
        #endregion

        #region Write File by byte Array

        public bool WriteFileBybytes( string filePathOut, byte[] fileByteContent )
        {
            bool bResult = false;
            string fileContent = string.Empty;
            try
            {
                fileContent = System.Convert.ToBase64String( fileByteContent );
                File.WriteAllText( filePathOut, fileContent );
                bResult = true;
            }
            catch ( System.Exception ) { }

            return bResult;
        }

        #endregion

        #region Xml Reading

        public static bool ReadXMLFile( string xmlFilePath )
        {
            bool bResult = false;
            XmlTextReader reader = null;

            try
            {
                reader = new XmlTextReader( xmlFilePath );
                bResult = true;
            }
            catch ( System.Exception ) { }
            
            while ( (null != reader) && reader.Read() )
            {
                switch ( reader.NodeType )
                {
                    case XmlNodeType.Element: // The node is an element.
                        System.Console.WriteLine( "<" + reader.Name + ">" );
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        System.Console.WriteLine( reader.Value );
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        System.Console.WriteLine( "</" + reader.Name + ">");
                        break;
                }
            }

            return bResult;
        }

        #endregion

        #region Finding Node

        public static bool XMLFindNode( string filePath, string nodeName, ref XmlNode xmlNode ) 
        {
            bool bResult = false;
            string xmlDoc = string.Empty;
            XmlDocument xml = new XmlDocument();
            xmlNode = null;

            try
            {
                xmlDoc = File.ReadAllText( filePath );
                xml.LoadXml( xmlDoc );

                if ( xml.HasChildNodes )
                {
                    xmlNode = FindNode( xml.FirstChild, nodeName );
                }
                bResult = true;
            }
            catch ( System.Exception ) { }

            return bResult;
        }

        public static XmlNode FindNode( XmlNode xmlCurrentNode, string nodeNameFound )
        {
            XmlNode xmlNodeResult = null;
            if ( null != xmlCurrentNode.SelectSingleNode( nodeNameFound ) )
            {
                xmlNodeResult = xmlCurrentNode.SelectSingleNode( nodeNameFound );
            }
            else if ( xmlCurrentNode.HasChildNodes )
            {
                return FindNode( xmlCurrentNode.FirstChild, nodeNameFound );
            }
            else if ( null != xmlCurrentNode.NextSibling )
            {
                return FindNode( xmlCurrentNode.NextSibling, nodeNameFound );
            }

            return xmlNodeResult;
        }
        #endregion
    }
}
